package com.one.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.one.constant.SystemConstant;
import com.one.mapper.CommentMapper;
import com.one.service.CommentService;
import com.one.util.DatabasePropertiesUtil;
import com.one.util.PageUtil;
import com.one.vo.CommentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;

/**
 * @ClassName: CommentServiceImpl
 * @Auther: yph
 * @Date: 2022/1/14 18:34
 * @Description: 评论业务层实现类
 */
@Service
public class CommentServiceImpl implements CommentService {

    @Autowired(required = false)
    private CommentMapper commentMapper;

    @Override
    public PageUtil<List<CommentVo>> queryCommentByProduct(Integer productId, String withPicture, Integer pageNum, Integer pageSize) {
        // 对分页数据进行判断，如果是0 或者null 就设为默认值
        if (pageNum == null || pageNum < 1) {
            pageNum = SystemConstant.PAGE_NUM;
        }
        if (pageSize == null || pageSize < 1) {
            pageSize = SystemConstant.PAGE_SIZE;
        }
        List<CommentVo> commentVos = commentMapper.queryCommentsByProductId(productId, !StringUtils.isEmpty(withPicture), pageNum, pageSize);

        commentVos.stream().forEach(commentVo -> {
            // 如果有图，并且有两张及以上图时 将数据进行解析并插入
            if (commentVo.getCommentImage() != null && commentVo.getCommentImage().contains(";")) {
                commentVo.setCommentImages(DatabasePropertiesUtil.stringToList(commentVo.getCommentImage()));
            } else {
                // 如果没有图片时 或只有一张图时
                commentVo.setCommentImages(Arrays.asList(commentVo.getCommentImage()));
            }
        });
        // 查询当前商品所有主评论
        Long totalRecords = commentMapper.selectMainCommentCountByProductId(productId, !StringUtils.isEmpty(withPicture));
        PageUtil<List<CommentVo>> pageUtil = new PageUtil<>();
        pageUtil.setPageNum(pageNum)
                .setPageSize(pageSize)
                .setTotalRecords(totalRecords)
                .setTotalPages(totalRecords % pageSize == SystemConstant.ZERO_CODE ? totalRecords / pageSize : (totalRecords / pageSize + 1))
                .setData(commentVos);
        return pageUtil;
    }

    @Override
    public List<CommentVo> queryCommentByParentId(Integer parentId) {
        // 根据评论id查询所有子评论，
        List<CommentVo> commentVos = commentMapper.queryCommentsByParentId(parentId);
        // 如果commentvo中有图片，进行解析
        commentVos.stream().forEach(commentVo -> {
            // 如果有图，并且有两张及以上图时 将数据进行解析并插入
            if (commentVo.getCommentImage() != null && commentVo.getCommentImage().contains(";")) {
                commentVo.setCommentImages(DatabasePropertiesUtil.stringToList(commentVo.getCommentImage()));
            } else {
                // 如果没有图片时 或只有一张图时
                commentVo.setCommentImages(Arrays.asList(commentVo.getCommentImage()));
            }
        });
        return commentVos;
    }
}
